package com.jiazhong.basic.atm;

import com.jiazhong.basic.atm.bean.Card;
import com.jiazhong.basic.atm.bean.TradeLog;
import com.jiazhong.basic.atm.dao.CardDAO;
import com.jiazhong.basic.atm.dao.TradeLogDAO;
import com.jiazhong.basic.atm.service.CardService;
import com.jiazhong.basic.atm.service.impl.CardServiceImpl;

import java.util.Scanner;

public class App {

    private Scanner scan = new Scanner(System.in);
    private CardDAO cdao = new CardDAO();
    private TradeLogDAO tldao = new TradeLogDAO();
    // ctrl+ alt +L
    private CardService cardService = new CardServiceImpl();

    private void reg() {
        String cardNo = (long) (Math.random() * 10000000000000000L + 1000000000000000L) + "";
        System.out.print("请输入密码：");
        String password = scan.next();
        System.out.print("请输入开卡金额：");
        double money = scan.nextDouble();
        System.out.print("请输入姓名：");
        String name = scan.next();
        Card card = new Card();
        card.setCardNo(cardNo);
        card.setMoney(money);
        card.setUsername(name);
        card.setPassword(password);
        cdao.save(card);
    }

    private void cunkuan() {
        System.out.print("请输入存款卡号：");
        String cardNo = scan.next();
        System.out.print("请输入存款金额：");
        double money = scan.nextDouble();
        System.out.print("请输入备注：");
        String desc = scan.next();
        String cunkuan = cardService.cunkuan(cardNo, money);
        System.out.println(cunkuan);
    }

    private void qukuan() {
        System.out.print("请输入取款卡号：");
        String cardNo = scan.next();
        System.out.print("请输入取款金额：");
        double money = scan.nextDouble();
        System.out.print("请输入备注：");
        String desc = scan.next();
        TradeLog tl = new TradeLog();
        tl.setDesc(desc);
        tl.setCardId(cardNo);
        tl.setTradeMoney(money);
        tl.setTradeId(2);
        // 更新余额
        cdao.update(cardNo, 0 - money);
        // 交易日志
        tldao.save(tl);
    }

    private int errorCount = 0;

    private void login() {
        // 输入账号和密码
        System.out.print("请输入取款卡号：");
        String cardNo = scan.next();
        System.out.print("请输入密码：");
        String password = scan.next();
        // 判断账号是否被锁定
        boolean block = cdao.isBlock(cardNo);
        if (block) {
            System.out.println("你的卡号：" + cardNo + "已经被锁定了，今天不能登陆！");
            return;
        }
        // 判断账号或者密码是否正确
        Card card = cdao.login(cardNo, password);
        if (card == null) {
            // 该卡号的错误次数+1
            cdao.errorCount(cardNo);
            System.out.println("账号或者密码错误");
            // 查询该卡的错误次数
            errorCount = cdao.getErrorCount(cardNo);
            if (errorCount == 3) {
                System.out.println("你今日次数已满，请明天再试！");
                // 锁卡
                cdao.blockCard(cardNo);
                return;
            }
            login();
            return;
        }
        System.out.println("欢迎" + card.getUsername() + "访问");
    }

    private void tran() {
        System.out.print("请输入卡号：");
        String cardNo = scan.next();
        System.out.print("请输入对方卡号：");
        String otherCardNo = scan.next();
        System.out.print("请输入交易金额：");
        double money = scan.nextDouble();
        String transfer = cardService.transfer(cardNo, otherCardNo, money);
        System.out.println(transfer);

    }

    public static void main(String[] args) {
        new App().login();
    }
}
/*
实现ATM的相关功能：
1. 搭建ATM的基本环境
2. 实现用户卡的注册
3. 实现存款操作
4. 实现取款操作
 */

/*
ATM :
1. 登陆
2. 存款
3. 取款
4. 查询
5. 转账
6. 退出

用户表（卡片信息）
交易记录表
交易类型表
create table card
(
	id int primary key auto_increment,
	cardNo varchar(20) unique,
	`password` varchar(200) not null,
	money DECIMAL(12,2) ,
	username varchar(20)
);
create table tradeType
(
	id int primary key auto_increment,
	type varchar(10)
);

insert into tradeType values(null,'存款');
insert into tradeType values(null,'取款');
insert into tradeType values(null,'转账');

create table tradeLog
(
	id int primary key auto_increment,
	cardId int,
	tradetime DATETIME DEFAULT(now()),
	tradeId int ,
	tradeMoney DECIMAL(12,2) ,
	otherCardid int,
	`desc` longtext
);
 */


/*
实现用户登陆功能，如果账号密码正确进入主界面并显示欢迎信息（欢迎具体某用户），
如果错误，提示并重新登陆，如果连续三次登陆失败，锁卡，禁止登陆（明天可以继续登陆）。
 */